----------------------------------------------------------------------------------------------------------------------------------------------------
--[[

]]--
----------------------------------------------------------------------------------------------------------------------------------------------------
---
    local assets =
    {
        Asset("ANIM", "anim/hutao_equipment_ghostly_march.zip"),
    }
----------------------------------------------------------------------------------------------------------------------------------------------------
-- 参数
    local MAX_FINITE_USES = 7           --- 最大使用次数
    local ON_GROUND_TRANS_TIME = 7      --- 放在地上 7 秒后转换
    local BASE_DAMAGE = 343             --- 基础伤害
    local PLANAR_DAMAGE = 100           --- 位面伤害
----------------------------------------------------------------------------------------------------------------------------------------------------
---
    local function onequip(inst, owner)
        owner.AnimState:OverrideSymbol("swap_object", "hutao_equipment_ghostly_march", "swap_object")
        owner.AnimState:Show("ARM_carry")
        owner.AnimState:Hide("ARM_normal")
    end

    local function onunequip(inst, owner)
        owner.AnimState:ClearSymbolBloom("swap_object")
        owner.AnimState:Hide("ARM_carry")
        owner.AnimState:Show("ARM_normal")
    end    
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 物品接受
    local function accept_test_fn(inst,item,doer,right_click)
        if item.prefab == "hutao_cooked_food_ghostly_march" then
            return true
        end
        return false
    end
    local function replica_fn(inst,replica_com)
        replica_com:SetTestFn(accept_test_fn)
        replica_com:SetText("hutao_equipment_ghostly_march",STRINGS.ACTIONS.APPLYCONSTRUCTION.REPAIR)
    end
    local function on_accept_fn(inst,item,doer)
        if inst.components.finiteuses:GetPercent() >= 1 then
            return false
        end
        item.components.stackable:Get():Remove()
        inst.components.finiteuses:Use(-1)
        return true
    end
    local function acceptable_com_install(inst)
        inst:ListenForEvent("HUTAO_OnEntityReplicated.hutao_com_acceptable",replica_fn)
        if not TheWorld.ismastersim then
            return
        end
        inst:AddComponent("hutao_com_acceptable")
        inst.components.hutao_com_acceptable:SetOnAcceptFn(on_accept_fn)
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 被转换成的瞬间执行函数
    local function on_ground_transform(inst)
        -- inst.components.finiteuses:SetUses(1)
        inst.components.finiteuses.current = 1
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 丢地上转换
    local function GetTalkerOffset(inst)
        return Vector3(0,-200,0)
    end
    local function on_ground_task(inst)
        if inst.components.hutao_data:Get("trans_time") == nil then
            inst.components.hutao_data:Set("trans_time",ON_GROUND_TRANS_TIME + 1)
        end
        if inst.components.inventoryitem.owner ~= nil then
            inst.components.hutao_data:Set("trans_time",ON_GROUND_TRANS_TIME + 1)
        else
            local time = inst.components.hutao_data:Add("trans_time",-1)
            inst.components.talker:Say(tostring(time))
            if time <= 0 then
                local x,y,z = inst.Transform:GetWorldPosition()
                inst:Remove()
                SpawnPrefab("sinkhole_warn_fx_3").Transform:SetPosition(x,y,z)
                SpawnPrefab("ash").Transform:SetPosition(x,y,z)
            end
        end
    end
    local function clear_talker(inst)
        inst.components.talker:Say(" ")
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- finiteuses hook
    local function finiteuses_hook(inst)
        local old_SetUses = inst.components.finiteuses.SetUses
        inst.components.finiteuses.SetUses = function(self,uses,...)
            local old_value = self.current
            local rets = {old_SetUses(self,uses,...)}
            local new_value = self.current
            self.inst:PushEvent("hutao_event.finiteuses_change",{
                old = old_value,
                new = new_value,
                down = new_value < old_value,
            })
            return unpack(rets)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 耐久度掉的时候
    local function finiteuses_down_event(inst,data)
        if data and data.down and not inst:HasTag("lootdropblock") then
            inst.components.lootdropper:SpawnLootPrefab("hutao_cooked_food_ghostly_march")
        end
    end
    local function on_finished(inst)
        if not inst:HasTag("lootdropblock") then
            inst.components.lootdropper:SpawnLootPrefab("hutao_cooked_food_ghostly_march")
        end
        inst:Remove()
    end
    local function SetLoopDropBlock(inst,flag)
        if flag then
            inst:AddTag("lootdropblock")
        else
            inst:RemoveTag("lootdropblock")
        end
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 本体
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddNetwork()

        MakeInventoryPhysics(inst)

        inst.AnimState:SetBank("hutao_equipment_ghostly_march")
        inst.AnimState:SetBuild("hutao_equipment_ghostly_march")
        inst.AnimState:PlayAnimation("idle",true)

        --weapon (from weapon component) added to pristine state for optimization
        inst:AddTag("weapon")

        MakeInventoryFloatable(inst)

        inst.entity:SetPristine()
        --------------------------------------------------------------------------
        -- talker
            inst:AddComponent("talker")
            inst.components.talker:SetOffsetFn(GetTalkerOffset)
        --------------------------------------------------------------------------
        ---
            acceptable_com_install(inst)
            inst.SetLoopDropBlock = SetLoopDropBlock
        --------------------------------------------------------------------------
        if not TheWorld.ismastersim then
            return inst
        end
        -----------------------------------------------------
        --- 
            inst:AddComponent("lootdropper")        
        -----------------------------------------------------
        --- 转换
            inst:AddComponent("hutao_data")
            inst:DoPeriodicTask(1,on_ground_task)
            inst:ListenForEvent("onpickup",clear_talker)
            inst:ListenForEvent("equipped",clear_talker)
        -----------------------------------------------------
        --- 武器
            inst:AddComponent("weapon")
            inst.components.weapon:SetDamage(BASE_DAMAGE)
        -----------------------------------------------------
        --- 位面伤害
            inst:AddComponent("planardamage")
            inst.components.planardamage:SetBaseDamage(PLANAR_DAMAGE)
        -----------------------------------------------------
        --- 物品
            inst:AddComponent("inspectable")
            inst:AddComponent("inventoryitem")
            -- inst.components.inventoryitem:ChangeImageName("cane")
            inst.components.inventoryitem.imagename = "hutao_equipment_ghostly_march"
            inst.components.inventoryitem.atlasname = "images/inventoryimages/hutao_equipment_ghostly_march.xml"
        -----------------------------------------------------
        --- 装备
            inst:AddComponent("equippable")
            inst.components.equippable:SetOnEquip(onequip)
            inst.components.equippable:SetOnUnequip(onunequip)
        -----------------------------------------------------
        --- 挖矿
            inst:AddComponent("tool")
            inst.components.tool:SetAction(ACTIONS.MINE,10000)
        -----------------------------------------------------
        --- 耐久度
            inst:AddComponent("finiteuses")
            inst.components.finiteuses:SetMaxUses(MAX_FINITE_USES)
            inst.components.finiteuses:SetUses(MAX_FINITE_USES)
            inst.components.finiteuses:SetOnFinished(on_finished)
            inst.components.finiteuses:SetConsumption(ACTIONS.MINE, 1)
            inst:ListenForEvent("on_ground_transform", on_ground_transform)
            finiteuses_hook(inst)
            inst:ListenForEvent("hutao_event.finiteuses_change",finiteuses_down_event)
        -----------------------------------------------------
        --- 耐久度复制体
            inst:AddComponent("hutao_com_finiteuses")
        -----------------------------------------------------
        --- 作祟
            MakeHauntableLaunch(inst)
        ---------------------------------------------------------
        return inst
    end

return Prefab("hutao_equipment_ghostly_march", fn, assets)
